/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_REFLECTION_TYPED_HPP
#define _CUBEFS_CORE_API_REFLECTION_TYPED_HPP

#include <cubefs/api/reflection/typed.hpp>
#include <cubefs/api/reflection/type.hpp>

#include <iostream>

#define CUBEFS_DECLARE_REFLECTION   \
public:                             \
    virtual Type* type() const {    \
        return type_;               \
    }                               \
                                    \
    static Type* MyType() {         \
        return type_;               \
    }                               \
                                    \
                                    \
private:                            \
    static CUBEFS_API Type* type_


#define CUBEFS_DEFINE_REFLECTION_TYPE(InterfaceName, ImplementationName)	\
Type* InterfaceName::MyType() {												\
	return ImplementationName::MyType();									\
}

#define CUBEFS_SOURCE "cubefs/core/api/reflection/typed.hpp:" CUBEFS_AT

namespace cubefs { namespace api {

template <typename Interface> class TypeImplT;
typedef TypeImplT<Type>	AbstractType;

template <typename Interface>
class TypedImplT: public Interface {
	typedef TypedImplT<Interface> 	Me;
	typedef Interface				Base;

public:
	typedef Interface InterfaceType;

	TypedImplT(): peer_(0), peer_flags_(0) {}

	virtual ~TypedImplT() throw () {
		try {
			notify(DESTROY_PEER);
		}
		catch (...) {} //nothrow

		try {
			CleanupPeer();
		}
		catch (...) {} //nothrow
	}

	virtual bool IsInstance(const Type *type) const {
		return this->type()->IsSameOrSubclass(type);
	}

	virtual BigInt GetPeer() const {
		return peer_;
	}

	virtual void SetPeer(BigInt peer) {
		peer_ = peer;
	}

	virtual Int GetPeerFlags() const {
		return peer_flags_;
	}

	virtual void SetPeerFlags(Int flags) {
		peer_flags_ = flags;
	}

	virtual const Int &peer_flags() const {
		return peer_flags_;
	}

	virtual Int &peer_flags() {
		return peer_flags_;
	}

	virtual bool managed() const {
		return true;
	}

	static void InitType() {
		type_ = new AbstractType(TypeNameFactory<Interface>::name(), NULL);
	}

	static void InitOperations();

	static void SetPeerNotifier(PeerNotifier notifier) {
		peerNotifier_ = notifier;
	}

	static void SetPeerCleanup(PeerCleanup cleanup) {
		cleanup_ = cleanup;
	}

	virtual void CleanupPeer() {
		if (cleanup_ != NULL) {
			cleanup_(this);
		}
	}

	virtual void notify(Int signal) {
		if (peerNotifier_ != NULL) {
			peerNotifier_(this, signal);
		}
	}

private:

	static PeerNotifier peerNotifier_;
	static PeerCleanup cleanup_;
	BigInt peer_;
	Int peer_flags_;

	CUBEFS_DECLARE_REFLECTION;
};

template <typename Interface>
Type* TypedImplT<Interface>::type_ 					= NULL;

template <typename Interface>
PeerNotifier TypedImplT<Interface>::peerNotifier_ 	= NULL;

template <typename Interface>
PeerCleanup TypedImplT<Interface>::cleanup_ 		= NULL;

typedef TypedImplT<Typed> 							TypedImpl;

}}
#undef CUBEFS_SOURCE
#endif
